import { doc_list_supplier_sample_post } from 'server/modules/4739'
import { message_board_todo_say_post } from 'server/modules/7537'
export default {
  methods: {
    async handlePlaceholders(props, str) {
      let replacedText = str
      // 替换cp占位符
      if (props.params.cp) {
        replacedText = replacedText.replace(/{{cp}}/g, props.params.cp)
      }

      // 替换操作 新增/修改占位符
      if (props.operationType) {
        replacedText = replacedText.replace(
          /{{operationType}}/g,
          props.operationType
        )
      }

      // 替换bom版本占位符
      if (props.params.bomVersion) {
        replacedText = replacedText.replace(
          /{{bomVersion}}/g,
          props.params.bomVersion
        )
      } else {
        replacedText = replacedText.replace(/{{bomVersion}}/g, '')
      }

      // 替换bom备注占位符
      let bomRemark = ''
      if (props.params.bomRemark) {
        bomRemark = ''.concat(',', props.params.bomRemark, '。')
      } else {
        bomRemark = '。'
      }
      replacedText = replacedText.replace(/{{bomRemark}}/g, bomRemark)

      // 替换大货工艺单版本占位符
      if (props.params.blukVerison) {
        replacedText = replacedText.replace(
          /{{blukVerison}}/g,
          props.params.blukVerison
        )
      } else {
        replacedText = replacedText.replace(/{{blukVerison}}/g, '')
      }

      // 替换供应商id
      if (props.params.supplierId) {
        const name = await this.transSupplierById(props.params.supplierId)
        replacedText = replacedText.replace(/{{supplierId}}/g, name)
      }
      return replacedText
    },
    /*
      cp，contentType=1，content，userIds [],cpCardId
     */
    async sendBoardMsg(params) {
      const { success, data, retInfo } =
        await message_board_todo_say_post(params)
      if (success) {
        console.log(data)
        this.$message.success(retInfo)
      } else {
        console.log(data)
        this.$message.error(retInfo)
      }
    },
    async transSupplierById(id) {
      const { success, data } = await doc_list_supplier_sample_post({
        id: id
      })
      if (success && data?.length > 0) {
        return data[0].supplierName
      } else {
        return false
      }
    }
  }
}
